package m3_publishsubscribe;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.UUID;

public class Consumer {
    public static void main(String[] args) throws Exception {
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140"); //www.wht6.cn
        f.setPort(5672); //默认端口可以省略
        f.setUsername("admin");
        f.setPassword("admin");
        // f.setVirtualHost("/xxxx");

        Channel c = f.newConnection().createChannel();
  //1.定义随机队列2.定义交换机3.绑定
//随机命名，非持久，独占，自动删除
        String queue = UUID.randomUUID().toString();
        c.queueDeclare(queue,false,true,true,null);
        c.exchangeDeclare("logs", BuiltinExchangeType.FANOUT);
        //第三个参数fanout交换机无效
        c.queueBind(queue,"logs","");
        //正常消费数据
        DeliverCallback deleverCallback = new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
            String msg =new String(message.getBody());
            System.out.println("收到："+msg);
            }
        };
        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {

            }
        };
        c.basicConsume(queue,true,deleverCallback,cancelCallback);
    }
    }
